<template>
  <div class="com-container">
    <div ref="stock" class="com-chart" />
  </div>
</template>
<script>
// import { getStock } from '../api/sys'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      stockChart: null,
      allData: null,
      timerId: null,
      currentIndex: 0
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.stockChart.dispose() // 销毁当前的图表
      this.initChart() // 更换主题后重新渲染
      this.screenAdaptation() // 屏幕适配
      this.upDataChart() // 更新图表数据
    }
  },
  created() {
    this.$socket.registerCallBack('stockData', this.getData)
  },
  mounted() {
    this.initChart()
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: ''
    })
    // this.getData()
    window.addEventListener('resize', this.screenAdaptation)
    this.screenAdaptation()
  },
  destroyed() {
    clearInterval(this.timerId)
    window.removeEventListener('resize', this.screenAdaptation)
    this.$socket.unRegisterCallBack('stockData')
  },
  methods: {
    initChart() {
      this.stockChart = this.$eCharts.init(this.$refs.stock, this.theme)
      const initOption = {
        title: {
          text: '▎库存和销量分析',
          left: 20,
          top: 20
        }
      }
      this.stockChart.setOption(initOption)
      this.stockChart.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.stockChart.on('mouseout', () => {
        this.startSetInterval()
      })
    },
    getData(res) {
      this.allData = res
      this.upDataChart()
      this.startSetInterval()
    },
    upDataChart() {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const currentData = this.allData.slice(start, end)
      const seriesArray = currentData.map((item, index) => {
        return {
          type: 'pie',
          center: centerArr[index],
          hoverAnimation: false, // 关闭鼠标移入到饼图时的动画效果
          labelLine: {
            show: false // 隐藏指示线
          },
          label: {
            position: 'center',
            color: colorArr[index][0]
          },
          data: [
            {
              value: item.sales,
              itemStyle: {
                color: new this.$eCharts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              name: item.name + '\n\n' + item.sales, // 销量
              value: item.stock, // 库存
              itemStyle: {
                color: '#333843'
              }
            }
          ]
        }
      })
      const dataOption = {
        series: seriesArray
      }
      this.stockChart.setOption(dataOption)
      this.startSetInterval()
    },
    startSetInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.upDataChart()
      }, 3000)
    },
    screenAdaptation() {
      const titleFontSize = this.$refs.stock.offsetWidth / 100 * 3.6
      const innerRadius = titleFontSize * 2.35
      const outRadius = innerRadius * 1.228
      const screenOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize * 1.2
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outRadius, innerRadius],
            label: {
              show: true,
              position: 'center',
              fontSize: titleFontSize / 1.6
            }
          },
          {
            type: 'pie',
            radius: [outRadius, innerRadius],
            label: {
              show: true,
              position: 'center',
              fontSize: titleFontSize / 1.6
            }
          },
          {
            type: 'pie',
            radius: [outRadius, innerRadius],
            label: {
              show: true,
              position: 'center',
              fontSize: titleFontSize / 1.6
            }
          },
          {
            type: 'pie',
            radius: [outRadius, innerRadius],
            label: {
              show: true,
              position: 'center',
              fontSize: titleFontSize / 1.6
            }
          },
          {
            type: 'pie',
            radius: [outRadius, innerRadius],
            label: {
              show: true,
              position: 'center',
              fontSize: titleFontSize / 1.6
            }
          }
        ]
      }
      this.stockChart.setOption(screenOption)
      this.stockChart.resize()
    }
  }
}
</script>
